﻿using System;
using System.Text;

namespace MangoCannon.Framework.Util
{
    public class HexUtil
    {
        public static byte[] FIRST_BEAT
        {
            get {
                ASCIIEncoding encoder = new ASCIIEncoding();
                return encoder.GetBytes("NACGAME");
            }
        }

        /// <summary>
        /// 字节数组转换到二进制字符串HEX
        /// </summary>
        /// <param name="ba">字节数组</param>
        /// <returns></returns>
        public static string bytesToHexString(byte[] ba)
        {
            StringBuilder sb = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
            {
                sb.AppendFormat("{0:x2}", b);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 字节数组转换到二进制整数
        /// </summary>
        /// <param name="ba">字节数组</param>
        /// <returns></returns>
        public static long bytesToHexInteger(byte[] ba)
        {
            long res = 0;
            try
            {
                res = long.Parse(bytesToHexString(ba));
            }
            catch (Exception)
            {
                return 0;
            }
            return res;
        }
    }
}
